import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/*
 * @author zzr
 * @date: 2025/05/10  17:09
 * @description: 自己实现一下多线程~~
 */
class MyThreadPoll2 {

    private List<Thread> list = new ArrayList<>();
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);

    public MyThreadPoll2(int n) {
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
                while (true) {
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
            list.add(thread);
        }
    }

    public void submit (Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class Demo13 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoll2 myThreadPoll2 = new MyThreadPoll2(4);
        int n = 1000;
        for (int i = 0; i < n; i++) {
            int t = i;
            myThreadPoll2.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("当前线程 " + t + Thread.currentThread().getName());
                }
            });
        }
    }
}
